<template>
<div>
<el-card   class="box-card" style="width:600px;margin:auto;text-align: center;">
    <span >修改密码</span>
</el-card>
<el-card class="box-card" style="width:600px;margin:auto;text-align: center;">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="原密码" prop="password" style="width:80%">
            <el-input type="password"  v-model="ruleForm.password" placeholder="请输入原密码"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="pass"  style="width:80%">
            <el-input  type="password" v-model="ruleForm.pass"  placeholder="请输入新密码"></el-input>
        </el-form-item>
         <el-form-item label="确认密码" prop="checkPass" style="width:80%">
            <el-input  type="password" v-model="ruleForm.checkPass" placeholder="请输入确认密码"></el-input>
        </el-form-item>
         <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">确认</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
    </el-form>
</el-card>
</div>
</template>
<script>
const axios = require("axios");
export default {

    data() {
      var validatePassword = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入原密码'));
        }else if(value.length>12 || value.length<6){
            callback(new Error('密码长度在6--12位'));
        } else {
          if (this.ruleForm.pass !== '') {
            this.$refs.ruleForm.validateField('pass');
          }
          callback();
        }
      };
       var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入新密码'));
        }else if (value == this.ruleForm.password) {
          callback(new Error('不能与原密码密码一致!'));
        }else if(value.length>12 || value.length<6){
            callback(new Error('密码长度在6--12位'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入新密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
            password: '',
            pass: '',
            checkPass: '',
        },
        rules: {
          password: [
            { validator: validatePassword, trigger: 'blur' }
          ],
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            //验证成功
              let formdata = new FormData();
              formdata.append("password",this.ruleForm.password);
               formdata.append("checkPass",this.ruleForm.checkPass);
             axios.post("http://192.168.6.189:8080/hpl-admin/pass",formdata).then(r=>{
                            if(r.data.code === 200){
                                        this.$message({
                                        message: '添加成功，这是一条成功消息',
                                            type: 'success'
                                            });	
                                            this.searche();
                                            this.cancelForm();                                  
                                    }else if(r.data.code === 403){
                                        this.$notify({
                                            title: '警告',
                                            message: r.data.msg,
                                            type: 'warning'
                                            }); 
                                    }else if(r.data.code === 500){
                                        this.$notify({
                                            title: '警告',
                                            message: r.data.msg,
                                            type: 'warning'
                                            }); 
                                    }else{
                                this.$notify({
                                    title: '警告',
                                    message: '系统繁忙！',
                                    type: 'warning'
                                    }); 
                            } 
                            })

          } else {
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>